package org.sqlproc.engine.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import org.hibernate.annotations.Type;
import org.hibernate.util.EqualsHelper;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
*
* BasicType for Ssn.
*/
@Embeddable
public class Ssn {
    @Column(name = "NUMBER", nullable = false, length = 20)
    @Length(max = 20)
    @NotEmpty
    private String number;
    @Column(name = "COUNTRY", nullable = false)
    @Type(type = "Country")
    @NotEmpty
    private Country country;

    public Ssn() {
    }

    public Ssn(String number, Country country) {
        super();
//        Validate.notNull(number);
        this.number = number;
        this.country = country;
    }

    public String getNumber() {
        return number;
    }

    /**
     * Creates a copy of this instance, but with another number.
     */
    public Ssn withNumber(String number) {
        if (EqualsHelper.equals(number, getNumber())) {
            return this;
        }
        return new Ssn(number, getCountry());
    }

    public Country getCountry() {
        return country;
    }

    /**
     * Creates a copy of this instance, but with another country.
     */
    public Ssn withCountry(Country country) {
        if (EqualsHelper.equals(country, getCountry())) {
            return this;
        }
        return new Ssn(getNumber(), country);
    }

    /**
     * This method is used by toString. It specifies what to
     * include in the toString result.
     * @return true if the field is to be included in toString
     */
//    protected boolean acceptToString(Field field) {
//        if (super.acceptToString(field)) {
//            return true;
//        } else {
//            if (field.getName().equals("country")) {
//                return true;
//            }
//            return false;
//        }
//    }

    /**
     * This method is used by equals and hashCode.
     * @return {@link #getSsnKey}
     */
    public Object getKey() {
        return getSsnKey();
    }

    /**
     * The natural key for the domain object is
     * a composite key consisting of several attributes.
     */
    public SsnKey getSsnKey() {
        return new SsnKey(getNumber(), getCountry());
    }

    /**
     * This is the natural key for the domain object.
     * It is a composite key consisting of several
     * attributes.
     */
    public static class SsnKey {
        private String number;
        private Country country;

        public SsnKey(String number, Country country) {
            this.number = number;

            this.country = country;

        }

        public String getNumber() {
            return number;
        }

        public Country getCountry() {
            return country;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof SsnKey)) {
                return false;
            }

            SsnKey other = (SsnKey) obj;

            if (!getNumber().equals(other.getNumber())) {
                return false;
            }
            if (!getCountry().equals(other.getCountry())) {
                return false;
            }
            return true;
        }

        public int hashCode() {
            int result = 17;
            result = 37 * result + computeHashCode(getNumber());
            result = 37 * result + computeHashCode(getCountry());
            return result;
        }

        private int computeHashCode(final Object o) {
            if (null == o) {
                return 19;
            }
            if (o.getClass().isArray()) {
                return 0;
            }
            return o.hashCode();
        }
    }

	public void setNumber(String number) {
		this.number = number;
	}

	public void setCountry(Country country) {
		this.country = country;
	}
    
    
}
